package com.foreveross.util;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class JSONUtil {

	public static  <T extends Object> List<T> jsonToClazzList(String str,TypeToken<List<T>> token){
		Gson gson = new Gson();
		
		Type typeOfT = token.getType();
        List<T> listT = gson.fromJson(str, typeOfT);
		return listT;
	}
	
	public static  <T extends Object> T jsonToClazz(String str,TypeToken<T> token){
		Gson gson = new Gson();
		
		Type typeOfT = token.getType();
        T t = gson.fromJson(str, typeOfT);
		return t;
	}
	
	public static <T extends Object> T jsonToClazz(String str, Class<T> clazz){
		Gson gson = new Gson();
        T t = gson.fromJson(str, clazz);
		return t;
	}
	
	public static String toJsonWithGson(Object obj)  
    {  
        Gson gson=new Gson();
        if (obj == null) {
        	return gson.toJson(new Object());
        }
        return gson.toJson(obj);  
    }  
    public static String toJsonWithGson(Object obj,Type type)  
    {  
        Gson gson=new Gson();  
        return gson.toJson(obj,type);  
    }  
    public static String toJsonWithGson(List list)  
    {  
        Gson gson=new Gson();  
        return gson.toJson(list);  
    }  
    public static String toJsonWithGson(List list,Type type)  
    {  
        Gson gson=new Gson();  
        return gson.toJson(list,type);  
    }  
    
    public static String toJsonWithGsonBuilder(List list)  
    {  
        Gson gson=new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy(list.getClass()))
        		.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
        		.setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
        
        return gson.toJson(list);  
    }
    
    public static String toJsonWithGsonBuilderBySerializeNulls(Object obj)  
    {  
        Gson gson=new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy(obj.getClass()))
        		.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
        		.setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
        
        return gson.toJson(obj);  
    }
    
    public static String toJsonWithGsonBuilderBySerializeNulls(List list)  
    {  
        Gson gson=new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy(list.getClass()))
        		.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
        		.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        
        return gson.toJson(list);  
    }
    
    public static String toJsonWithGsonBuilder(Object obj)  
    {  
        Gson gson=new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy(obj.getClass()))
        		.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
        		.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        
        return gson.toJson(obj);  
    }
}
